<?php

namespace App\Model;

use App\Models\Archive\Archive;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table = 'category';
    protected $primaryKey = 'cate_id';
    public $timestamps = false;
    protected $guarded = [];
    //递归无限级分类
    public static function sort($arr,$parent_id = 0,$lev=0)
    {
        static $list = array();
        foreach($arr as $v) {
            if($v['cate_pid'] == $parent_id) {
                $v['lev'] = $lev;
                $list[] = $v;
                self::sort($arr,$v['cate_id'],$lev+1);
            }
        }
        return $list;

    }

    public static function get_pid($cate_id)
    {
        static $pid = array();
        $self = self::where('cate_id',$cate_id)->first();
        if($self->cate_pid!=0){
            $pid[] = $self->cate_pid;
            self::get_pid($self->cate_pid);
        }

        return implode(',',$pid);

    }
    public function archive()
    {
        return $this->hasMany('App\Models\Archive\Archive', 'category_id', 'cate_id');
    }

/*    public static function recommend()
    {
        //精彩推荐
        $cate1 = self::where('cate_name','精彩推荐')->first();
        if($cate1){
            $son = Category::where('cate_pid',$cate1->cate_id)->count();
            if($son){
                $cate_son = Category::where('cate_pid',$cate1->cate_id)->get();
                $cate_arr1 = [];
                foreach($cate_son as $v){
                    $cate_arr1[] = $v->cate_id;
                }
                array_push($cate_arr1,$cate1->cate_id);
                $cate_ids = implode(',',$cate_arr1);
                $article_archives =   Archive::whereIn('category_id',$cate_ids)->ofPattern('review')
                    ->orderBy('updated_at','desc')
                    ->take(6)
                    ->get()
                ;
            }else{
                $article_archives =   Archive::where('category_id',$cate1->cate_id)->ofPattern('review')
                    ->orderBy('updated_at','desc')
                    ->take(6)
                    ->get();
            }
        }else{
            $article_archives = false;
        }
        return $article_archives;
    }

    public static function game()
    {
        $cate3 = self::where('cate_name','游戏攻略')->first();
        $category_id2 = $cate3 ? $cate3->cate_id : null;
        if($cate3){
            $son = Category::where('cate_pid',$cate3->cate_id)->count();
            if($son){
                $cate_son = Category::where('cate_pid',$cate3->cate_id)->get();
                $cate_arr3 = [];
                foreach($cate_son as $v){
                    $cate_arr3[] = $v->cate_id;
                }
                array_push($cate_arr3,$cate3->cate_id);
                $cate_ids3 = implode(',',$cate_arr3);
                $game_archives =   Archive::whereIn('category_id',$cate_ids3)->ofPattern('review')
                    ->orderBy('updated_at','desc')
                    ->take(3)
                    ->get()
                ;
            }else{
                $game_archives =   Archive::where('category_id',$cate3->cate_id)->ofPattern('review')
                    ->orderBy('updated_at','desc')
                    ->take(3)
                    ->get()
                ;
            }

        }else{
            $game_archives = false;
        }
        return $game_archives;
    }*/
}
